var map;
var geocoder;
var centerChangedLast;
var reverseGeocodedLast;
var currentReverseGeocodeResponse;
var marker;
function initialize(ll,ln,zoom){
    var latlng=new google.maps.LatLng(ll,ln);
    var myOptions={
        zoom:zoom,
        center:latlng,
        mapTypeId:google.maps.MapTypeId.ROADMAP
        };
    map=new google.maps.Map(document.getElementById("map_canvas"),myOptions);
    geocoder=new google.maps.Geocoder();
    setupEvents();
    centerChanged();
}
function setupEvents(){
    reverseGeocodedLast=new Date();
    centerChangedLast=new Date();
    setInterval(function(){
        if((new Date()).getSeconds()-centerChangedLast.getSeconds()>1){
            if(reverseGeocodedLast.getTime()<centerChangedLast.getTime())
                reverseGeocode();
        }
    },1000);
    google.maps.event.addListener(map,'center_changed',centerChanged);
    google.maps.event.addDomListener(document.getElementById('crosshair'),'dblclick',function(){
        map.setZoom(map.getZoom()+1);
    });
}
function getCenterLatLngText(){
    return'('+map.getCenter().lat()+', '+map.getCenter().lng()+')';
}
function centerChanged(){
    centerChangedLast=new Date();
    var latlng=getCenterLatLngText();
    document.getElementById('formatedAddress').innerHTML='';
    currentReverseGeocodeResponse=null;
}
function reverseGeocode(){
    reverseGeocodedLast=new Date();
    geocoder.geocode({
        latLng:map.getCenter()
        },reverseGeocodeResult);
}
function reverseGeocodeResult(results,status){
    currentReverseGeocodeResponse=results;
    if(status=='OK'){
        if(results.length==0){
            document.getElementById('formatedAddress').innerHTML='None';
        }else{
            document.getElementById('formatedAddress').innerHTML=results[0].formatted_address;
        }
    }else{
        document.getElementById('formatedAddress').innerHTML='Error';
    }
}
function geocode(){
    var address=document.getElementById("address").value;
    geocoder.geocode({
        'address':address,
        'partialmatch':true
    },geocodeResult);
}
function geocodeResult(results,status){
    if(status=='OK'&&results.length>0){
        map.fitBounds(results[0].geometry.viewport);
    }
}
function addMarkerAtCenter(){
    if(marker!=null)
        marker.setMap(null);
    marker=new google.maps.Marker({
        position:map.getCenter(),
        map:map,
        draggable: true
    });
    google.maps.event.addListener(marker, 'dragend', function(){
        actualizarll();
    });
    google.maps.event.addListener(marker,'click',function(){
        actualizarll();
    });
    actualizarll();
}
function addExistentMarker(ll,ln){
    var latlng=new google.maps.LatLng(ll,ln);
    marker=new google.maps.Marker({
        position:latlng,
        map:map,
        draggable: true
    });
    google.maps.event.addListener(marker, 'dragend', function(){
        actualizarll();
    });
    google.maps.event.addListener(marker,'click',function(){
        actualizarll();
    });
    actualizarll();
}
function actualizarll(){
    $('#PropertyLatitude').attr('value',marker.position.lat());
    $('#PropertyLongitude').attr('value',marker.position.lng());
}
$(document).ready(function(){
    initialize(ll,ln,zoom);
    addExistentMarker(ll,ln);
    $('#address').keypress(function(evento){
        if(evento.which==13){
            geocode();
            return false;
        }
    });
});